package Data::Timeline::Formatter::SimpleTable;

use strict;
use warnings;
use Text::SimpleTable;


our $VERSION = '0.01';


use base qw(Data::Timeline::Formatter);


__PACKAGE__->mk_array_accessors(qw(columns));


sub format {
    my ($self, $timeline) = @_;
    my $table = Text::SimpleTable->new(
        [ 20, 'timestamp' ], $self->columns,
    );

    my @col_types = map { $_->[1] } $self->columns;
    for my $entry ($timeline->entries) {
        my @row = (sprintf "%s" => $entry->timestamp);
        for my $col_type (@col_types) {
            if ($entry->type eq $col_type) {
                push @row => $entry->description;
            } else {
                push @row => '';
            }
        }
        $table->row(@row);
    }

    print $table->draw;
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - Print timeline entry types side-by-side in a text table

=head1 SYNOPSIS

    {% p.package %}->new(
        columns => [ [ 40, 'iscrobbler' ], [ 40, 'svk' ] ],
    )->format($timeline);

=head1 DESCRIPTION

This class is a timeline formatter. It takes a timeline containing entries of
one or more entry types and a column definition. The column definition says
for each column how wide it should be and which type of entries it should
contain. The formatter's C<format()> method will then print a simple text
table containing the requested columns, with a column for the timestamp at the
beginning.

The column definition is a list of C<[ $width, $type ]> pairs. So for the
example in the synopsis, the first column would contain the timestamp, the
second column would be 40 characters wide and contain C<iscrobbler> entries,
produced by L<Data::Timeline::IScrobbler>, and the third column would also
be 40 characters wide and contain C<svk> entries, produced by
L<Data::Timeline::SVK>.

{% p.write_inheritance %}

=head1 METHODS

=over 4

{% p.write_methods %}

=item format

    $formatter->format($timeline);

Takes a timeline and formats it as described above.

=back

{% PROCESS standard_pod %}

=cut

